package com.wing.warehouse.controller.admin.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.warehouse.dao.WarehouseFactoryShelfDao;
import com.wing.warehouse.model.entity.WarehouseFactoryShelfEntity;
import com.wing.warehouse.model.entity.WarehouseFactoryShelfSpaceEntity;
import com.wing.warehouse.model.entity.WarehouseFactoryShelfVO;
import com.wing.warehouse.model.request.WarehouseFactoryShelfForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 仓库货架-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class WarehouseFactoryShelfServiceImpl extends ServiceImpl<WarehouseFactoryShelfDao, WarehouseFactoryShelfEntity> implements WarehouseFactoryShelfService{

    @Autowired
    private WarehouseFactoryShelfSpaceService shelfSpaceService;

    @Override
    public int add(WarehouseFactoryShelfForm warehouseShelfForm) {

        //校验

        WarehouseFactoryShelfEntity warehouseShelf =new WarehouseFactoryShelfEntity();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(WarehouseFactoryShelfEntity.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(warehouseShelfForm, warehouseShelf, copyOptions);

        return baseMapper.insert(warehouseShelf);
    }

    @Override
    public List<WarehouseFactoryShelfVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<WarehouseFactoryShelfVO> pageList4app(Page page, Map<String, Object> params) {
        List<WarehouseFactoryShelfVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public WarehouseFactoryShelfVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public boolean save(WarehouseFactoryShelfEntity warehouseShelf) {
        baseMapper.insert(warehouseShelf);
        Integer columns=warehouseShelf.getColumnNum();
        Integer layers=warehouseShelf.getLayerNum();
        Integer columnLen=columns.toString().length();
        Integer layerLen=layers.toString().length();
        String shelfSn=warehouseShelf.getCode();
        WarehouseFactoryShelfSpaceEntity warehouseShelfSpace=new WarehouseFactoryShelfSpaceEntity();
        BeanUtils.copyProperties(warehouseShelf,warehouseShelfSpace);
//        warehouseShelfSpace.setId(null);
//        executorService.submit(new Runnable() {
//            @Override
//            public void run() {
        System.out.println(layers+"\t"+columns);
        for (Integer i=1;i<=layers;i++){
            for (Integer j=1;j<=columns;j++){
                System.out.println(i+"\t"+j);
                String sn=shelfSn+"-"+getNum(i,layerLen)+"-"+getNum(j,columnLen);

                warehouseShelfSpace.setCode(sn);
                warehouseShelfSpace.setShelfId(warehouseShelf.getId());
                warehouseShelfSpace.setId(null);
                shelfSpaceService.save(warehouseShelfSpace);
            }
        }
//            }
//        });
        return true;
    }

    private String getNum(Integer val,Integer len){
        String zero="";
        Integer valLen=val.toString().length();
        for (Integer i=0;i<len-valLen;i++){
            zero+="0";
        }
        return zero+val;
    }

    @Override
    public boolean update(WarehouseFactoryShelfEntity warehouseShelf) {
        baseMapper.updateById(warehouseShelf);
        Integer columns=warehouseShelf.getColumnNum();
        Integer layers=warehouseShelf.getLayerNum();
        Integer columnLen=columns.toString().length();
        Integer layerLen=layers.toString().length();
        String shelfSn=warehouseShelf.getCode();
        WarehouseFactoryShelfSpaceEntity warehouseShelfSpace=new WarehouseFactoryShelfSpaceEntity();
        BeanUtils.copyProperties(warehouseShelf,warehouseShelfSpace);
        shelfSpaceService.disableByShelfId(warehouseShelf.getId());
        for (Integer i=1;i<=layers;i++){
            for (Integer j=1;j<=columns;j++){
                String sn=shelfSn+"-"+getNum(i,layerLen)+"-"+getNum(j,columnLen);
                warehouseShelfSpace.setCode(sn);
                warehouseShelfSpace.setShelfId(warehouseShelf.getId());
                warehouseShelfSpace.setStatus(WarehouseFactoryShelfSpaceEntity.Status.enable);
                List<WarehouseFactoryShelfSpaceEntity> shelfSpaces=shelfSpaceService.list(new QueryWrapper<WarehouseFactoryShelfSpaceEntity>().eq("code",sn));
                if (shelfSpaces.size()==0){
                    warehouseShelfSpace.setId(null);
                    shelfSpaceService.save(warehouseShelfSpace);
                }else {
                    warehouseShelfSpace.setId(shelfSpaces.get(0).getId());
                    warehouseShelfSpace.setWarehouseId(shelfSpaces.get(0).getWarehouseId());
                    warehouseShelfSpace.setWarehouseAreaId(shelfSpaces.get(0).getWarehouseAreaId());
                    shelfSpaceService.updateShelfSpaceById(warehouseShelfSpace);
                }

            }
        }
        return true;
    }
}
